#include "view_messages.h"
#include "app_trace.h"
#include "battery_ctrl.h"
#include "blecom.h"
#include "datetime.h"
#include "display_ctrl.h"
#include "display_interface.h"
#include "font_mngr.h"
#include "heart_rate_interface.h"
#include "lfs_file_manager_auto.h"
#include "math.h"
#include "platform_assert.h"
#include "rtc.h"
#include "sport_his.h"
#include "weather_ctrl.h"
#include "font/font.h"
#include "common/common.h"
#include "view_messages.inc"
#include "prompt/view_prompt_message.h"
#include "utf8_to_unicode.h"
#include "arabic_reshaper.h"
#include <wchar.h>
void view_message_base_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.com_data.i_temp[0] = ancs_msg_get_count();
    }
    // #warning debug;
    uint32_t message_num = disp_ctrl.com_data.i_temp[0];
    draw_on_chip_gradients_image(6, 46, zip_image_circle, 0xfde0, 0xfbc0);
    if (message_num != 0)
    {
        draw_on_chip_gray_image_filter_color(6, 46, zip_image_msg, RGB565_COLOR_WHITE);
    }
    else
    {
        draw_on_chip_gray_image_filter_color(6, 46, zip_image_no_msg, RGB565_COLOR_WHITE);
    }
}
#define TEXT_X1 0
#define TEXT_Y1 33
#define TEXT_X2 (79)
#define TEXT_Y2 (159)
void view_message_page_display(void)
{
    uint16_t *unicode_str = (uint16_t *)(&disp_ctrl.com_data.i_temp[4]);
	  int x = 0;
	  int y = 0;
    if (disp_ctrl.loop_cnt == 0) //更新数据需求
    {
        disp_ctrl.auto_refresh_time = 1000;
        if (ancs_disp_ctrl.new_flag)
        {
            ancs_message_package_t msg = {0};
            ancs_msg_get(&msg, ancs_disp_ctrl.read_num);
            ancs_disp_ctrl.message_type = msg.ancs_msgType;
            utf8_string_to_unicode_string((uint8_t *)msg.ancs_msg.appAttrMessage, (uint8_t *)unicode_str,
                                          ((uint8_t *)(disp_ctrl.com_data.i_temp)) + sizeof(disp_ctrl.com_data.i_temp) - (uint8_t *)unicode_str);
            disp_ctrl.com_data.int16_temp[4] = arabic_reshaper(unicode_str);
            ancs_disp_ctrl.message_len = wcslen(unicode_str) * 2;
            ancs_disp_ctrl.new_flag = 0;
            ancs_disp_ctrl.disp_offset = 0;
        }
        disp_ctrl.com_data.int16_temp[0] = ancs_disp_ctrl.disp_offset;
        disp_ctrl.com_data.int16_temp[1] = 0;
        disp_ctrl.com_data.int16_temp[2] = 0;
        ancs_disp_ctrl.next_offset = draw_off_chip_string_calculate(TEXT_X1, TEXT_Y1, TEXT_X2, TEXT_Y2, unicode_str, 16) & 0xffff;
    }
    icon_info_t const *icon_info = 0;
    if (ancs_disp_ctrl.message_type < sizeof(message_icons) / sizeof(message_icons[0]))
    {
        icon_info = message_icons[ancs_disp_ctrl.message_type];
    }
    else
    {
        icon_info = message_icons[ATTP_ID_OTHERS];
    }
    x = 26;
    y = 2;
    if (icon_info->start_color != 0)
    {
        if (icon_info->end_color != 0)
        {
            draw_on_chip_gradients_image(x, y, icon_info->image, icon_info->start_color, icon_info->end_color);
        }
        else
        {
            draw_on_chip_gray_image(x, y, icon_info->image, icon_info->start_color);
        }
    }
    else
    {
        draw_on_chip_image(x, y, icon_info->image);
    }
    uint32_t temp16 = draw_off_chip_string(TEXT_X1, TEXT_Y1 + disp_ctrl.com_data.int16_temp[1], TEXT_X2, TEXT_Y2,
                                           (uint16_t *)((uint8_t *)unicode_str + disp_ctrl.com_data.int16_temp[0] + disp_ctrl.com_data.int16_temp[2]), 16, &disp_ctrl.com_data.int16_temp[4]);
    if (temp16)
    {
        disp_ctrl.com_data.int16_temp[2] += (temp16 & 0xffff);
        disp_ctrl.com_data.int16_temp[1] += ((temp16 >> 16) * (FONT16_SIZE + FONT_SIZE_VERTICAL_INTERVAL));
    }
    if (disp_ctrl.com_data.int16_temp[0] != 0) //非首页的继续显示
    {
        if ((disp_ctrl.com_data.int16_temp[0] + ancs_disp_ctrl.next_offset) < ancs_disp_ctrl.message_len) //非最后一页
        {
            draw_on_chip_image(TEXT_X2 - 16, TEXT_Y2 - 16, IMAGE_USER_FONT_16X16_DOT);
        }
    }
    else
    {
        if (ancs_disp_ctrl.next_offset < ancs_disp_ctrl.message_len) //多页
        {
            draw_on_chip_image(TEXT_X2 - 16, TEXT_Y2 - 16, IMAGE_USER_FONT_16X16_DOT);
        }
    }
}
void view_messages_display(int view)
{
    if (view == view_message_base)
    {
        view_message_base_display();
    }
    else
    {
        view_message_page_display();
    }
}
view_event_result_t view_message_base_event(event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
    {
        int message_num = ancs_msg_get_count();
        if (message_num == 0)
        {
            return view_event_result_handled;
        }
        else
        {
            return view_event_result_not_handled;
        }
    }
    }
    return view_event_result_not_handled;
}

view_event_result_t view_message_page_event(event_param_t const *param)
{
    switch (param->event)
    {
    case event_view_entry:
        if (param->previous_view != view_message_page)
        {
            ancs_disp_ctrl.new_flag = 1;
            ancs_disp_ctrl.read_num = 0;
            return view_event_result_handled;
        }
        break;
#ifdef EVENT_LEVEL_UP
    case event_move_level_up:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
#ifdef USE_TOUCHPAD
    case BUTTON_EVENT_TP_TAP_LONG:
#endif
        display_show(view_messages, DISP_MODE_NORMAL);
        return view_event_result_handled;
#ifdef EVENT_PREVIOUS_VIEW
    case event_move_previous_view:
    {
        if (ancs_msg_get_count() == 0)
        {
            display_show(view_time, DISP_MODE_NORMAL);
        }
        else
        {
            ancs_disp_ctrl.disp_offset = 0;
            ancs_disp_ctrl.new_flag = 1;
            if (ancs_disp_ctrl.read_num == 0)
            {
            }
            else
            {
                ancs_disp_ctrl.read_num--;
            }
            display_show(view_message_page, DISP_MODE_NORMAL);
        }
        return view_event_result_handled;
    }
#endif
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
#endif
    {

        if (ancs_msg_get_count() == 0)
        {
            display_show(view_messages, DISP_MODE_NORMAL);
        }
        else
        {
            int new_offset = ancs_disp_ctrl.disp_offset + ancs_disp_ctrl.next_offset; //偏移计算
            if (new_offset >= ancs_disp_ctrl.message_len)
            {
                if (ancs_disp_ctrl.read_num + 1 >= ancs_msg_get_count())
                {
#ifdef EVENT_NEXT_VIEW
#else
                        ancs_disp_ctrl.read_num = 0;
                        ancs_disp_ctrl.disp_offset = 0;
                        ancs_disp_ctrl.new_flag = 1;
                        display_show(view_message_page, DISP_MODE_DOWN_MOVE);
#endif
                }
                else
                {
                    ancs_disp_ctrl.read_num++;
                    ancs_disp_ctrl.disp_offset = 0;
                    ancs_disp_ctrl.new_flag = 1;
                    display_show(view_message_page, DISP_MODE_DOWN_MOVE);
                }
            }
            else
            {
                ancs_disp_ctrl.disp_offset = new_offset;
                display_show(view_message_page, DISP_MODE_DOWN_MOVE);
            }
        }
        return view_event_result_handled;
    }

    default:
        break;
    }
    return view_event_result_not_handled;
}
view_event_result_t view_messages_event(int view, event_param_t const *param)
{
    if (view == view_message_base)
    {
        return view_message_base_event(param);
    }
    else
    {
        return view_message_page_event(param);
    }
}
